package org.example.completed.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.completed.domain.PaideventReservation;
import org.example.completed.domain.Reservation;
import org.example.completed.domain.User;
import org.example.completed.mapper.PaideventReservationMapper;
import org.example.completed.mapper.UserMapper;
import org.example.completed.service.PaideventReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2025-04-12
 */
@Service
public class PaideventReservationServiceImpl extends ServiceImpl<PaideventReservationMapper, PaideventReservation> implements PaideventReservationService {

    @Autowired
    private PaideventReservationMapper paideventReservationMapper;

    @Autowired
    private UserMapper userMapper;


    @Override
    @Transactional
    public void register(String userId, String contact) {
    // 检查该 userId 是否已经存在预约记录
        List<Reservation> existingReservations = paideventReservationMapper.selectByUserId(userId);
        if (!existingReservations.isEmpty()) {
            // 如果已经存在预约记录，返回 false
            throw new IllegalArgumentException("用户已经存在预约记录");
        }
        PaideventReservation reservation = paideventReservationMapper.selectById("1");

        // 判断是否已满员
        if (reservation.getTotalNumber().equals(reservation.getApplyNumber())) {
            throw new IllegalArgumentException("报名人数已满");
        }

        // 查询用户信息
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new IllegalArgumentException("用户不存在");
        }

        // 判断用户是否为 VIP
        if (!"vip".equals(user.getRights())) {
            // 非 VIP 用户需要付费
            BigDecimal price = reservation.getPrice();
            if (user.getMoney().compareTo(price) < 0) {
                throw new IllegalArgumentException("余额不足");
            }
            // 扣除用户余额
            user.setMoney(user.getMoney().subtract(price));
            userMapper.updateById(user);
        }

        // 更新活动的报名人数
        reservation.setApplyNumber(reservation.getApplyNumber() + 1);
        paideventReservationMapper.updateById(reservation);

        // 新增一条报名记录，复制原活动信息并设置新的用户 ID 和联系电话
        PaideventReservation newReservation = new PaideventReservation();
        newReservation.setContact(contact); // 使用传入的联系电话
        newReservation.setName(reservation.getName());
        newReservation.setVisitTime(reservation.getVisitTime());
        newReservation.setTotalNumber(reservation.getTotalNumber());
        newReservation.setApplyNumber(reservation.getApplyNumber());
        newReservation.setPrice(reservation.getPrice());
        newReservation.setUserId(userId);
        newReservation.setUserAcoount(user.getUserAcoount());
        paideventReservationMapper.insert(newReservation);
    }
}
